#!/bin/bash

#File:set_es_env.sh(原创)

# 关闭防火墙
chkconfig iptables off  > /dev/null 
service iptables stop > /dev/null


es_setup_dir="/opt/elasticsearch-5.5.0"


# 添加并授权ES的使用给新用户
useradd es_admin
passwd es_admin
chown -R es_admin "$es_setup_dir"
chgrp -R es_admin "$es_setup_dir"
echo "添加并授权ES的使用给新用户(es_admin)完成"


# 设置用户能打开的最大文件数(65536)
echo -e "\n\n* soft nofile 65536" >> /etc/security/limits.conf
echo -e "* hard nofile 131072" >> /etc/security/limits.conf
echo -e "* soft nproc 2048" >> /etc/security/limits.conf
echo -e "* hard nproc 4096" >> /etc/security/limits.conf
ulimit -n 65536
echo "设置用户能打开的最大文件数(65536)完成"


# 设置系统最大系统内存参数(vm.max_map_count=262144) 
if [ `grep -c "vm.max_map_count" /etc/sysctl.conf` -eq '0' ]; then
    echo -e "\n\n\nvm.max_map_count=262144" >>/etc/sysctl.conf
	sysctl -p >/dev/null 2>&1 # 执行命令后不管是命令输出结果或是异常都不打印任何信息
	echo "配置vm.max_map_count=262144完成"
else
	cat /etc/sysctl.conf | grep vm.max_map_count
    echo "请手动配置vm.max_map_count=262144"
fi


# linxu内核版本不支持SecComp，修改elasticsearch配置文件不进行自动检测
echo -e "\n\nbootstrap.memory_lock: false" >> "$es_setup_dir/config/elasticsearch.yml" 
echo -e "bootstrap.system_call_filter: false\n\n" >> "$es_setup_dir/config/elasticsearch.yml" 
echo "设置elasticsearch配置文件,不进行自动检测SecComp完成"


# 设置network.host: 0.0.0.0
echo -e "\n\nnetwork.host: 0.0.0.0\n\n" >> "$es_setup_dir/config/elasticsearch.yml" 
echo "设置network.host: 0.0.0.0完成"


